package com.singularity.onelabel.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.singularity.onelabel.R;
import com.singularity.onelabel.capsule.CreateCapsuleActiviity;
import com.singularity.onelabel.goal.CreateGoalActivity;
import com.singularity.onelabel.lable.CreateLabelActivity;
import com.singularity.onelabel.publish.PublishDialog;

import java.util.List;

/**
 * Created by Lenovo on 2018/11/29.
 */

public class HomeTabSelectUtils implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private RadioGroup radioGroup;
    private List<Fragment> fragmentList;
    private int container;
    private FragmentManager fragmentManager;
    private Activity activity;

    public HomeTabSelectUtils(Activity activity,RadioGroup radioGroup, List<Fragment> fragmentList, int container, FragmentManager fragmentManager) {
        this.radioGroup = radioGroup;
        this.activity = activity;
        this.fragmentList = fragmentList;
        this.container = container;
        this.fragmentManager = fragmentManager;
        //获取FragmentTransaction类，并设置默认选中第一个页面
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 添加一个Fragment
        fragmentTransaction.add(container, fragmentList.get(0));
        //提交
        fragmentTransaction.commit();
        // 设置第一个item被选中
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        // 设置RadioGroup的监听
        radioGroup.setOnCheckedChangeListener(this);
        // 设置底部中间加号的按钮的点击事件
        radioGroup.findViewById(R.id.publish_entry_layout).setOnClickListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            if (i < 2) {
                //点击第一第二个按钮的时候设置fragment
                Fragment fragment = fragmentList.get(i);
                setFragment(fragment, i, id);
            } else if (i > 2) {
                // 点击第四个和第五个按钮的
                Fragment fragment = fragmentList.get(i - 1);
                setFragment(fragment, i, id);
            }
        }
    }

    private void setFragment(Fragment fragment, int i, int id) {
        if (radioGroup.getChildAt(i).getId() == id) {
            // 显示fragment
            showFragment(fragment);
        } else {
            // 隐藏fragment
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.hide(fragment);
            ft.commit();
        }
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (!fragment.isAdded()) {
            fragmentTransaction.add(container, fragment);
        }
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.publish_entry_layout:
                // 弹出弹窗选择发布的类型
                PublishDialog dialog = new PublishDialog(activity);
                // 设置点击弹窗的按钮的回调
                dialog.setButtonClickListener(mDialogButtonClickListener);
                //用show方法来显示弹窗
                dialog.show();
                break;
            default:
                break;
        }
    }

    private PublishDialog.OnDialogButtonClickListener mDialogButtonClickListener = new PublishDialog.OnDialogButtonClickListener() {
        @Override
        public void onLabelClicked() {
            // 回调Intent跳转到编写标签的类
            Intent intentLabel = new Intent(activity, CreateLabelActivity.class);
            activity.startActivity(intentLabel);
        }
        @Override
        public void onCapsuleClicked() {
            // 回调Intent跳转到编写胶囊的类
            Intent intentCapsule = new Intent(activity, CreateCapsuleActiviity.class);
            activity.startActivity(intentCapsule);
        }
        @Override
        public void onGoalClicked() {
            // 回调Intent跳转到编写目标的类
            Intent intentGoal = new Intent(activity, CreateGoalActivity.class);
            activity.startActivity(intentGoal);
        }
    };
}
